/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2010
 */

package com.tech15.core.service;

import java.util.ArrayList;
import java.util.List;

import javacommon.base.BaseManager;
import javacommon.base.EntityDao;

import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.page.PageRequest;

import com.tech15.core.dao.GroupDao;
import com.tech15.core.dao.GroupUserDao;
import com.tech15.core.model.Group;
import com.tech15.core.model.GroupUser;
import com.tech15.core.vo.query.GroupQuery;
import com.tech15.core.vo.query.GroupUserQuery;

/**
 * @author badqiu email:badqiu(a)gmail.com
 * @version 1.0
 * @since 1.0
 */

@Service
@Transactional
public class GroupManager extends BaseManager<Group,Long>{

	private GroupDao groupDao;
	private GroupUserDao groupUserDao;
	
	public GroupUserDao getGroupUserDao() {
		return groupUserDao;
	}
	public void setGroupUserDao(GroupUserDao groupUserDao) {
		this.groupUserDao = groupUserDao;
	}
	public void setGroupDao(GroupDao dao) {
		this.groupDao = dao;
	}
	public EntityDao getEntityDao() {
		return this.groupDao;
	}
	
	@Transactional(readOnly=true)
	public List<Group> selectGroup(Integer isshfw) {
		return groupDao.selectGroup(isshfw);
	}
	@Transactional(readOnly=true)
	public List<Group> selectGroupByUserid(Long userid) {
		return groupDao.selectGroupByUserid(userid);
	}
	
	@Transactional(readOnly=true)
	public Page findPage(GroupQuery query) {
		return groupDao.findPage(query);
	}
	
	public List<Group> findAll(Integer appid){
		return groupDao.findAll(appid);
	}
	public Group getById(Long id){
		Group pg = groupDao.getById(id);
		pg.setLevel(false);
		return pg;
	}
	
	/** 插入数据 */
	public void save(Group pg) throws DataAccessException{
		getEntityDao().save(pg);
		getEntityDao().flush();
		if(pg.getSelectUserIds() != null && !"".equals(pg.getSelectUserIds())){
			String selectuser = pg.getSelectUserIds().replace(" ","");
			String[] selectuserids = selectuser.split(",");
			List<GroupUser> list = new ArrayList<GroupUser>();
			for(int i=0;i<selectuserids.length;i++){
				GroupUser pgu = new GroupUser();
				pgu.setUserid(new Long(selectuserids[i]));
				pgu.setPgid(pg.getPgid());
				list.add(pgu);
			}
			getGroupUserDao().getHibernateTemplate().saveOrUpdateAll(list);
		}
	}
	
	/** 更新数据 */
	public void update(Group pg) throws DataAccessException{
		getEntityDao().update(pg);
		if(pg.getSelectUserIds() != null && !"".equals(pg.getSelectUserIds())){
			getGroupUserDao().delete("delete sys_group_user a where a.pgid="+pg.getPgid()+" and a.userid not in ("+pg.getSelectUserIds()+")");
			String selectuser = pg.getSelectUserIds().replace(" ","");
			String[] selectuserids = selectuser.split(",");
			List<GroupUser> list = new ArrayList<GroupUser>();
			for(int i=0;i<selectuserids.length;i++){
				GroupUser pgu = new GroupUser();
				pgu.setUserid(new Long(selectuserids[i]));
				pgu.setPgid(pg.getPgid());
				boolean res = groupUserDao.isUnique(pgu, "pgid,userid");
				if(res){
					list.add(pgu);
				}
			}
			getGroupUserDao().getHibernateTemplate().saveOrUpdateAll(list);
		}else{
			getGroupUserDao().delete("delete sys_group_user a where a.pgid="+pg.getPgid());
		}
	}
	public Page searchGroupUsers(GroupUserQuery query) {
		return groupDao.searchGroupUsers(query);
	}
	@Transactional(readOnly=true)
	public List<GroupUser> selectGroupUsersByPgid(Long pgid) {
		return groupDao.selectGroupUsersByPgid(pgid);
	}
	
}
